package com.liwbn.rbac.core.orm.dialect;

/**
 * Postgre 数据库 方言
 * 
 * @author liwb 2015年9月24日 下午8:10:53
 */
public class PostgreDialect extends Dialect {

    /**
     * 得到分页的SQL
     * 
     * @param offset
     *            偏移量
     * @param limit
     *            位置
     * @return 分页SQL
     */
    public String getLimitString(String querySelect, int offset, int limit) {
        querySelect = getLineSql(querySelect);
        String sql = querySelect + " LIMIT " + limit + " OFFSET " + offset;
        return sql;
    }

    public String getCountString(String querySelect) {
        querySelect = getLineSql(querySelect);
        int orderIndex = getLastOrderInsertPoint(querySelect);

        int formIndex = getAfterFormInsertPoint(querySelect);
        String select = querySelect.substring(0, formIndex);

        // 如果SELECT 中包含 DISTINCT 只能在外层包含COUNT
        if (select.toLowerCase().indexOf("select distinct") != -1
                || querySelect.toLowerCase().indexOf("group by") != -1) {
            return new StringBuffer(querySelect.length())
                    .append("select count(1) count from (")
                    .append(querySelect.substring(0, orderIndex))
                    .append(" ) t").toString();
        } else {
            return new StringBuffer(querySelect.length())
                    .append("select count(1) count ")
                    .append(querySelect.substring(formIndex, orderIndex))
                    .toString();
        }
    }
}
